/**
 * Copyright (C) 2013 ChangSha XNS Science & Technology CO,.LTD. All rights reserved.
 * XNS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/**
 * Copyright (C) 2002-2013 WUZEWEN. All rights reserved.
 * WUZEWEN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 * This software is the confidential and proprietary information of
 * WuZeven, Personal. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with WuZeven.
 */

package com.kingcore.pda.core.servlet;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kingcore.pda.core.util.DebugUtil;

/**
 * <p>java类文件的说明...</p>
 * @author Zeven/wzw on Oct 6, 2013
 * @version	1.0
 * @see		Object#equals(java.lang.Object)
 * @since	JDK5
 */

public class FileViewServlet extends HttpServlet {

	
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//super.doPost(req, resp);		
		
		//指定页面不缓存
		resp.setDateHeader("Expires", -1);//IE
		resp.setHeader("Cache-Control", "no-cache");
		resp.setHeader("Pragma", "no-cache");
		
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=UTF-8");
		String fileUri = null;
		String filePath = req.getParameter("file");
		DebugUtil.testEncoding(filePath);
		//filePath = new String(filePath.getBytes("iso8859-1"), "utf-8");  //utf-8
		filePath = new String(filePath.getBytes("iso8859-1"), "gbk");  //utf-8
		System.out.println("=========filePath====="+filePath);
		fileUri = req.getRealPath(filePath);
		System.out.println("=============="+fileUri);
		downLoad(fileUri, resp);
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public static boolean downLoad(String fileUri, HttpServletResponse response) {

		String filename;
		File tfile = new File(fileUri);
		filename = tfile.getName();
		String contentType = " ";
		if (filename.toLowerCase().indexOf(".xls ") > 0
				|| filename.toLowerCase().indexOf(".xlsx ") > 0) {
			
			contentType = "application/vnd.ms-excel; "
					+ "charset=UTF-8;attachment;   filename= " + filename;
		} else if(filename.toLowerCase().indexOf(".doc ") > 0
				|| filename.toLowerCase().indexOf(".docx ") > 0) {
			
			contentType = "application/x-msword";
		} else if(filename.toLowerCase().indexOf(".txt ") > 0 ) {
			
			contentType = "text/html";
		} else {
			contentType = "application/vnd.ms-zip; "
					+ "charset=UTF-8;attachment;   filename= " + filename;
		}
		byte[] buffer = new byte[8192];
		// Create the download files
		int bytesRead = 0;
		if (tfile != null) {
//			response.setContentType(contentType);
//			response.setHeader("content-disposition ",
//					"attachment;   filename=\" " + filename + "\" ");
			//response.setContentType(contentType); 
			response.setContentType("application/x-msword"); 

			BufferedOutputStream oStream = null;
			FileInputStream is = null;
			try {
				is = new FileInputStream(tfile);
				oStream = new BufferedOutputStream(
						response.getOutputStream());
				// Get file stream
				while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {
					oStream.write(buffer, 0, bytesRead);
				}
				oStream.flush();
				response.flushBuffer();
				System.out.println("读取文件流完成！");
			} catch (IOException ioe) {
				ioe.printStackTrace();
				System.out.println("ssss==> " + ioe.toString());
				return false;
				// errors.add( "nofile ",new ActionMessage( "file.not.exsit.error "));
				// this.saveErrors(request,errors);
				// return (new ActionForward(mapping.getInput()));
			} catch (Throwable e) {
				e.printStackTrace();
				return false;

				// errors.add( "nofile ",new ActionMessage( "file.not.exsit.error "));
				// this.saveErrors(request,errors);
				// return (new ActionForward(mapping.getInput()));
			} finally {

				try {
					if (oStream != null) {
						oStream.close();
					}
				} catch (IOException e) { 
				}
				try {
					if (is != null) {
						is.close();
					}
				} catch (IOException e) { 
				}
				
			}
		}

		return true;
	}

	/**
	 * <p>java方法的说明...</p>
	 * @author Zeven on Oct 6, 2013
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
